package javaLanguage.javaMultithread;

import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class threadSynchronize {
    static final ArrayList<Integer> container = new ArrayList<>();

    public static void main(String[] args) throws InterruptedException {
        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(3, 3, 1000, TimeUnit.MILLISECONDS, new LinkedBlockingDeque<>());
        CountDownLatch synchronizeFlag = new CountDownLatch(10);
        for(int i = 0; i < 10; i ++) {
            threadPool.execute(() ->{
                try{
                    container.add(0);
                    int random = new Random().nextInt();
                    Thread.sleep(random);
                } catch (Exception e){
                    e.printStackTrace();
                } finally {
                    synchronizeFlag.countDown();
                }
            });

        }
        synchronizeFlag.await();
        for (Integer integer : container) {
            System.out.printf("输出容器：%d\n", integer);
        }
    }
}
